local vector2d = require("Vector2D")
local gravity = require("gravity3")

M = {}

local newPlayer = function(imageSheet, sheet, x, y,game)
    local player = display.newCircle(x, y, 7)
    player:setFillColor( 255, 0, 0 )
    player.planeta = nil
    player.jump = false
    player.radio = 7
    physics.addBody(player, "dynamic", { density = 0.4, friction = 0.0, bounce = 0.0, radius = 7 })
    player.cx=player.x
    player.cy=player.y
    player.isVisible = false
    
    player.playerSprite = display.newImageRect(imageSheet,sheet,20,20)
    
    
    local colision = 0
    local pressTimer
    
    --impulso
    local function impulsar(event)
        
        if (event.phase == "began") then
            if (player.planeta ~= nil and player.jump == false ) then                
                player.jump = true
                gravity.jumpI(player)
                --player.planeta = nil
                colision = 10
                --print(scale)
                --if(0.90 < scale and scale <= 1)then
                --    targetScale = 0.70
                --    print(targetScale)
                --end
                
                local function listener( event )
                    gravity.jump(player)
                    player.planeta = nil
                    player.jump = false
                end
                pressTimer = timer.performWithDelay(180, listener )
            end
        elseif (event.phase == "ended") then
            if pressTimer then
                timer.cancel(pressTimer)
                pressTimer = nil
            end
        end
        --[[
        if(colision == 0 and player.jump == true)then
             gravity.jump(player)
                player.planeta = nil
                player.jump = false      
        end
        --]]
    end
    
    Runtime:addEventListener("touch", impulsar)
    
    
    local function onPlayerCollision(self, event)
        if (event.phase == "began") then
            if(event.other.tipo == "PLANETA" )then
                if(colision == 0 and  player.planeta==nil)then
                    player.planeta = event.other
                end 
                if(colision == 0 and player.jump == true)then
                    player.jump = false
                end
            elseif(event.other.tipo == "COLLECTOR" )then
                event.other:removeSelf( )   
            end
            
        elseif (event.phase == "ended") then
        end
    end
    
    player.collision = onPlayerCollision
    player:addEventListener("collision", player)
    
    local function angleBetween ( srcObj, dstObj )
        local xDist = dstObj.x-srcObj.x ; 
        local yDist = dstObj.y-srcObj.y
        local angleBetween = math.deg( math.atan( yDist/xDist ) )
        if ( srcObj.x < dstObj.x ) then 
            angleBetween = angleBetween+90 
        else 
            angleBetween = angleBetween-90 
        end
        return angleBetween
    end
    
    local updatePlayer = function()
        --sincronizacion con sprite
        player.playerSprite.x = player.x
        player.playerSprite.y = player.y
        
        if(player.planeta~= nil)then
            player.playerSprite.rotation = angleBetween ( player.planeta, player )
        else
            local vx,vy = player:getLinearVelocity()
            player.playerSprite.rotation = angleBetween ( { x = 0, y = 1 }, { x = vx, y = vy } )
        end
        --control de colision
        if(colision>0)then
            colision = colision -1
        end        
        
        --velocidad en planeta
        gravity.speed(player)
        --aplico gravedad
        gravity.applyGravity(player, game)
        
    end
    
    player.updatePlayer = updatePlayer
    
    return player
end

M.newPlayer = newPlayer

return M